'''
@author: 502552742
'''
from server.operations import checkAccount, doRegister, searchAccount,\
    randomSelect
from django.http.response import JsonResponse
import traceback
import random

# code: 0:success 1:error 2:warning 3:notify

def login(request):
    returnDict = {}
    
    if request.method == 'POST':
        try:
            username = request.POST['username']
            password = request.POST['password']
            value = checkAccount(username, password)
            if value==1:
                returnDict["code"] = "1"
                returnDict["msg"] = "Error:Error password!"
            elif value==2:
                returnDict["code"] = "2"
                returnDict["msg"] = "Warning:Please register!"
            else:
                returnDict["code"] = "0"
                returnDict["msg"] = "Success!"
                request.session["username"] = username
        except:
            returnDict["code"] = "1"
            returnDict["msg"] = traceback.format_exc()
    else:
        returnDict["code"] = "1"
        returnDict["msg"] = "Error:Request method is not post!"
        
    return JsonResponse(returnDict)

def logout(request):
        returnDict = {}
        
        if request.method == "POST":
            try:
                username = request.session["username"]
                if "userName" in request.session:
                    del request.session["username"]
                    returnDict["code"] = "0"
                    returnDict["msg"] = "Success!"
                    returnDict["userName"] = username
                else:
                    returnDict["code"] = "2"
                    returnDict["userName"] = username
                    returnDict["msg"] = "Don't need to logout!"
            except:
                returnDict["code"] = "1"
                returnDict["msg"] = traceback.format_exc()
        else:
            returnDict["code"] = "1"
            returnDict["msg"] = "Error:Request method is not post!"
        
        return JsonResponse(returnDict)
        
def register(request):
    returnDict = {}
    if request.method == 'POST':
        try:
            username = request.POST['username']
            password = request.POST['password']
            email = request.POST['email']
            value = doRegister(username, password, email)
            if value==0:
                returnDict["code"] = "0"
                returnDict["msg"] = "Success!"
            else:
                returnDict["code"] = "2"
                returnDict["msg"] = "This account is existed!"
        except:
            returnDict["code"] = "1"
            returnDict["msg"] = traceback.format_exc()
    else:
        returnDict["code"] = "1"
        returnDict["msg"] = "Error:Request method is not post!"
    
    return JsonResponse(returnDict)
        
def welcome(request):
    returnDict = {}
    if request.method == 'POST':
        try:
            if "username" in request.session:
                returnDict["code"] = "0"
                returnDict["msg"] = "Success!"
                returnDict["username"] = request.session["username"]
            else:
                returnDict["code"] = "2"
                returnDict["msg"] = "Warning:Need to login!"
        except:
                returnDict["code"] = "1"
                returnDict["msg"] = traceback.format_exc()
    else:
        returnDict["code"] = "1"
        returnDict["msg"] = "Error:Request method is not post!"
        
    return JsonResponse(returnDict)

def searchUser(request):
    returnDict = {}
    
    if request.method == 'POST':
        try:
            username = request.POST['username']
            email = request.POST['email']
            phone = request.POST['phone']
            accounts = searchAccount(username, email, phone)
            if accounts.__len__() < 1:
                returnDict["code"] = "3"
                returnDict["msg"] = "Warning:No Result!"
            else:
                returnDict["code"] = "0"
                returnDict["msg"] = accounts
        except:
            returnDict["code"] = "1"
            returnDict["msg"] = traceback.format_exc()
    else:
        returnDict["code"] = "1"
        returnDict["msg"] = "Error:Request method is not post!"
        
    return JsonResponse(returnDict)

def searchUserRandom(request):
    returnDict = {}
    
    if request.method == 'POST':
        try:
            username = request.POST['username']
            email = request.POST['email']
            phone = request.POST['phone']
            accounts = searchAccount(username, email, phone)
            if accounts.__len__() < 1:
                returnDict["code"] = "3"
                returnDict["msg"] = "Warning:No Result!"
            else:
                returnDict["code"] = "0"
                amount = accounts.__len__()
                random_number = random.randint(0, amount-1)
                returnDict["msg"] = accounts[random_number]
        except:
            returnDict["code"] = "1"
            returnDict["msg"] = traceback.format_exc()
    else:
        returnDict["code"] = "1"
        returnDict["msg"] = "Error:Request method is not post!"
        
    return JsonResponse(returnDict)